package com.xrit.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xrit.sunnyday.constant.MessageConstant;
import com.xrit.sunnyday.entity.Result;
import com.xrit.sunnyday.pojo.Member;
import com.xrit.sunnyday.service.MemberService;
import com.xrit.sunnyday.service.SetMealService;
import com.xrit.sunnyday.utils.DateUtils;
import org.apache.ibatis.jdbc.Null;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * 类名称: ReportController
 * 类描述:TODO
 * 创建人: czb
 * 创建时间:2022/7/8 上午 11:24
 * Version 1.0.0
 */
@RestController
@RequestMapping("/report")
public class ReportController {

    @Reference
    private MemberService memberService;

    @Reference
    private SetMealService setMealService;

    @GetMapping("/getMemberReport")
    public Result getMemberReport(){
        List<String> months = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR,-1);
        for (int i = 0; i < 12; i++) {
            try {
                calendar.add(Calendar.MONTH,1);
                Date time = calendar.getTime();
                String s = DateUtils.parseDate2String(time, "yyyy-MM-dd");
                months.add(s);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
       List<Integer> memberCount= memberService.getMemberReport(months);
        HashMap<String, Object> map = new HashMap<>();
        map.put("months",months);
        map.put("memberCount",memberCount);
        return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
    }


    @GetMapping("/getSetMealReport")
    public Result getSetMealReport(){
        ArrayList<String> setmealNames = new ArrayList<>();
        List<Map<String,Object>> setmealCount = setMealService.getSetmealReport();
        for (Map<String, Object> map : setmealCount) {
            setmealNames.add(String.valueOf(map.get("name")));
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("setmealNames",setmealNames);
        map.put("setmealCount",setmealCount);
        return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,map);
    }

    @GetMapping("/getBusinessReportData")
    public Result getBusinessReportData(){
        HashMap<String, Object> map = memberService.finByDate();
        return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS,map);
    }

    @GetMapping("/exportBusinessReport")
    public void  exportBusinessReport(HttpServletResponse res , HttpServletRequest req){
        //读取模板
        String template = req.getSession().getServletContext().getRealPath("/template/report_template.xlsx");
        //获取数据
        HashMap<String, Object> map = memberService.finByDate();

        ServletOutputStream  os=null;
        XSSFWorkbook xw=null;
        FileInputStream fis= null;
        try {
             fis = new FileInputStream(template);
             os = res.getOutputStream();
             xw = new XSSFWorkbook(fis);
            XSSFSheet sheetAt = xw.getSheetAt(0);
            //今天日期
            sheetAt.getRow(2).getCell(5).setCellValue((String) map.get("reportDate"));

            //会员统计
            // todayNewMember: 0, //今天新增会员数
            sheetAt.getRow(4).getCell(5).setCellValue((Integer) map.get("todayNewMember"));
            // totalMember: 0,//总会员数
            sheetAt.getRow(4).getCell(7).setCellValue((Integer) map.get("totalMember"));
            // thisWeekNewMember : 0,//本周新增会员数
            sheetAt.getRow(5).getCell(5).setCellValue((Integer) map.get("thisWeekNewMember"));
            // thisMonthNewMember: 0,//本月新增会员数
            sheetAt.getRow(5).getCell(7).setCellValue((Integer) map.get("thisMonthNewMember"));


            //预约到诊数据统计
            // todayOrderNumber: 0,//今日预约数
            sheetAt.getRow(7).getCell(5).setCellValue((Integer) map.get("todayOrderNumber"));
            //  todayVisitsNumber: 0,//今日到诊数
            sheetAt.getRow(7).getCell(7).setCellValue((Integer) map.get("todayVisitsNumber"));
            //  thisWeekOrderNumber: 0,//本周预约数
            sheetAt.getRow(8).getCell(5).setCellValue((Integer) map.get("thisWeekOrderNumber"));
            // thisWeekVisitsNumber: 0,//本周到诊数
            sheetAt.getRow(8).getCell(7).setCellValue((Integer) map.get("thisWeekVisitsNumber"));
            //  thisMonthOrderNumber: 0,//本月预约数
            sheetAt.getRow(9).getCell(5).setCellValue((Integer) map.get("thisMonthOrderNumber"));
            //   thisMonthVisitsNumber: 0,//本月到诊数
            sheetAt.getRow(9).getCell(7).setCellValue((Integer) map.get("thisMonthVisitsNumber"));

            //热门套餐
            List<Map<String,Object>> hotSetMeal = (List<Map<String, Object>>) map.get("hotSetMeal");
            int Row = 12;
            if (null!=hotSetMeal) {
                for (Map<String, Object> setMeal : hotSetMeal) {
                    sheetAt.getRow(Row).getCell(4).setCellValue((String) setMeal.get("name"));
                    sheetAt.getRow(Row).getCell(5).setCellValue((Long) setMeal.get("setmeal_count"));
                    BigDecimal proportion = (BigDecimal) setMeal.get("proportion");
                    sheetAt.getRow(Row).getCell(6).setCellValue(proportion.doubleValue());
                    sheetAt.getRow(Row).getCell(7).setCellValue((String) setMeal.get("remark"));
                    Row++;
                }
            }
            //文件名格式
            String fileName = new String("运营统计数据报表.xlsx".getBytes(), StandardCharsets.ISO_8859_1);

            // 设置头信息，告诉浏览器，是带附件的，文件下载
            res.setHeader("Content-Disposition","attachment;filename=" + fileName);
            // 设置内容体的文件格式是excel
            res.setContentType("application/vnd.ms-excel");


            xw.write(os);
            os.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                os.close();
                xw.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
